﻿using AutoMapper;
using SX.BaseService.Repository.Core;
using SX.BaseService.Repository.Entity;
using SX.BaseService.Service.Interface;
using SX.Core;
using SX.Core.Auth;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SX.BaseService.Service.Implement.Auth
{

    public class AuthService :DbContext, IAuthService
    {
        private readonly ILoginInfo _loginInfo;
        private readonly IRepository<SysUserEntity> _userRepository;
        private IMapper _mapper;
        public AuthService(ILoginInfo loginInfo, IRepository<SysUserEntity> userRepository,IMapper mapper)
        {
            _mapper = mapper;
            _loginInfo = loginInfo;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 获得认证信息
        /// </summary>
        /// <returns></returns>
        public async Task<IApiResult> GetAuthInfo()
        {
            if (_loginInfo.UserId.Length <= 0)
            {
                return ApiResult.NotOk("未登录！");
            }

            var user = await _userRepository.GetId(_loginInfo.UserId);
            if (user == null)
                return ApiResult.NotOk("账户不存在");
            if (user.Status == 0)
                return ApiResult.NotOk("账户未启用");

            var model = new AuthInfoOutPut
            {
                Adress = user.Adress,
                CreateTime = user.CreateTime,
                Sex = user.Sex,
                HeadPic = user.HeadPic,
                Id = user.Id,
                Password = user.Password,
                Phone = user.Phone,
                Remarks = user.Remarks,
                TrueName = user.TrueName,
                UpLoginDate = user.UpLoginDate,
                UserName = user.UserName
            };

            var entities = new List<SysPermissionEntity>();

            //查询用户拥有的菜单
            var sql = @"select distinct  sp.Id,sp.Name,sp.Code,sp.Path,sp.Icon,sp.Sort,sp.CreateTime,sp.Remarks,sp.ParentId from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sur.SysUserId=@UserId  and sp.Type=0 and sp.Enabled=1 order by sort asc";

            //查询用户拥有的API权限
            var sql2 = @"select distinct sp.code from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sur.SysUserId=@UserId  and sp.Type=1 and sp.Enabled=1";

            if (user.Id == "d4eb3aa3-2b7c-7b05-8623-3a088875309b")
            {
                 sql = @"select distinct  sp.Id,sp.Name,sp.Code,sp.Path,sp.Icon,sp.Sort,sp.CreateTime,sp.Remarks,sp.ParentId from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sp.Type=0 and sp.Enabled=1 order by sort asc";
                 sql2 = @"select distinct sp.code from SysPermission sp 
            left join SysRolePermission srp on sp.Id=srp.PermissionId 
            left join SysUserRole sur on sur.SysRoleId=srp.RoleId  where sp.Type=1 and sp.Enabled=1";
            }


            entities = await Db.Ado.SqlQueryAsync<SysPermissionEntity>(sql, new { UserId = user.Id });
            model.Permissions = await Db.Ado.SqlQueryAsync<string>(sql2, new { UserId = user.Id });
            //菜单列表   
            var all = _mapper.Map<List<UserMenu>>(entities);
            
            model.Menus = all;
            foreach (var item in model.Menus)
            {
                item.NamePath = item.Name;
            }
            //账户Web系统配置config
            var config = await Db.Queryable<SysUserConfigEntity>().Where(x=>x.SysUserId==user.Id).SingleAsync();
            model.config = config;


            model.MsgCount = 0;
            return ApiResult.Ok(model);
        }
    }
}
